package uit.bus;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class ShowRoute extends MapActivity {
	public MapView map;
	public String strDi;
	public String strDen;
	public int matuyen1, matuyen2;
	public int maluotdi;
	public int sttluotdi;
	public int maluotden;
	public int sttluotden;
	public int trangthai = 0;
	public DBAdapter db;
	public GeoPoint startGP, endGP;
	public String diemchuyen;
	public ArrayList<GeoPoint> listGP = new ArrayList<GeoPoint>();

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.route);
		map = (MapView) findViewById(R.id.map);
		// map = HCMBusActivity.mapView;
		map.setBuiltInZoomControls(true);
		strDi = KetQuaTimKiem.noiDi;
		strDen = KetQuaTimKiem.noiDen;
		Bundle bundle = getIntent().getExtras();
		db = new DBAdapter(this);
		if (bundle != null) {
			trangthai = bundle.getInt("trangthai");
			if (trangthai == 1) {
				matuyen1 = bundle.getInt("matuyen1");
				showDirect(strDi, strDen, 1);
			} else {
				matuyen1 = bundle.getInt("matuyen1");
				matuyen2 = bundle.getInt("matuyen2");
				diemchuyen = bundle.getString("diemchuyen");
				showDirect(strDi, diemchuyen, 1);
				showDirect(diemchuyen, strDen, 2);
			}
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void showDirect(String noidi, String noiden, int color) {
		db.open();
		Cursor csdi = db.getLatLongDD(noidi);
		Cursor csden = db.getLatLongDD(noiden);
		if (csdi.moveToFirst()) {
			startGP = new GeoPoint((int) (csdi.getDouble(1) * 1E6),
					(int) (csdi.getDouble(2) * 1E6));
		}
		if (csden.moveToFirst()) {
			endGP = new GeoPoint((int) (csden.getDouble(1) * 1E6),
					(int) (csden.getDouble(2) * 1E6));
		}
		db.close();
		String pairs[] = getDirectionData(startGP, endGP);
		String[] lngLat = pairs[0].split(",");
		GeoPoint gp1;
		GeoPoint gp2 = startGP;

		for (int i = 1; i < pairs.length; i++) {
			lngLat = pairs[i].split(",");
			gp1 = gp2;
			// watch out! For GeoPoint, first:latitude, second:longitude

			gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
					(int) (Double.parseDouble(lngLat[0]) * 1E6));
			map.getOverlays().add(new DirectionPathOverlay(gp1, gp2, color));
		}
	}

	private String[] getDirectionData(GeoPoint start, GeoPoint end) {

		String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
				+ String.valueOf(start.getLatitudeE6() / 1E6) + ","
				+ String.valueOf(start.getLongitudeE6() / 1E6) + "&daddr="
				+ String.valueOf(end.getLatitudeE6() / 1E6) + ","
				+ String.valueOf(end.getLongitudeE6() / 1E6)
				+ "&ie=UTF8&0&om=0&output=kml";

		Log.d("URL", urlString);
		Document doc = null;
		HttpURLConnection urlConnection = null;
		URL url = null;
		String pathConent = "";

		try {

			url = new URL(urlString.toString());
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.connect();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(urlConnection.getInputStream());

		} catch (Exception e) {
		}

		NodeList nl = doc.getElementsByTagName("LineString");
		for (int s = 0; s < nl.getLength(); s++) {
			Node rootNode = nl.item(s);
			NodeList configItems = rootNode.getChildNodes();
			for (int x = 0; x < configItems.getLength(); x++) {
				Node lineStringNode = configItems.item(x);
				NodeList path = lineStringNode.getChildNodes();
				pathConent = path.item(0).getNodeValue();
			}
		}
		String[] tempContent = pathConent.split(" ");
		return tempContent;
	}
}
